//
//  GraphLayer.m
//
//  source file for layer that shows a graph of high scores
//

#import "GraphLayer.h"
#import "SimpleAudioEngine.h"
#import "StatisticsLayer.h"
#import "MainMenuLayer.h"

@implementation GraphLayer

// returns a scene object containing the current layer
// this essentially loads the "screen" for a
// particular section or part of the game
+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    GraphLayer *mainMenu = [GraphLayer node];
    [scene addChild: mainMenu];
    
    return scene;
}

// handler for statistics button when pressed
-(void)statisticsPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[StatisticsLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
    
}

// handler for main menu button when pressed
-(void) mainMenuPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[MainMenuLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(id) init
{
    if ((self = [super init])) {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(winSize.width / 2 - 300, winSize.height / 2 - 270, 600, 500)];
        
        NSString *urlAddress = @"http://chart.apis.google.com/chart?cht=lc&chd=t:3,56,24,43,67,94,100&chs=600x500&chxt=x,y&chco=B28D00&chls=5";
        urlAddress = [urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:requestObj];
        
        CCSprite *bg = [CCSprite spriteWithFile:@"menuBackground.png"];
        bg.position = ccp(winSize.width / 2, winSize.height / 2);
        [self addChild:bg z:0];
        
        CCLabelTTF *graphLabel = [CCLabelTTF labelWithString:@"High Scores Graph" fontName:@"Marker Felt" fontSize:64];
        graphLabel.position = ccp(winSize.width / 2, winSize.height - 64);
        graphLabel.color = ccc3(255, 255, 255);
        [self addChild:graphLabel];
        
        CCMenuItemImage *statisticsBtn = [CCMenuItemImage itemWithNormalImage:@"statisticsButton.png"
                                                                selectedImage:@"statisticsButtonHighlight.png" target:self selector:@selector(statisticsPressed)];
        
        CCMenuItemImage *menuBtn = [CCMenuItemImage itemWithNormalImage:@"mainMenuButton.png"
                                                          selectedImage:@"mainMenuButtonHighlight.png" target:self selector:@selector(mainMenuPressed)];
        
        CCMenu *menu = [CCMenu menuWithItems:statisticsBtn, menuBtn, nil];
        [menu alignItemsVerticallyWithPadding:10.0];
        menu.position = ccp(winSize.width / 2, statisticsBtn.contentSize.height / 2 + 40);
        [self addChild:menu];


        [[[CCDirector sharedDirector] view] addSubview:_webView];
    }
    
    return self;
}

-(void) dealloc
{
    [_webView removeFromSuperview];
    [super dealloc];
}

@end
